import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/404'] // no redirect whitelist
// 前置守卫
router.beforeEach(async(to, from, next) => {
  document.title = getPageTitle(to.meta.title)
  NProgress.start()// 添加进度条
  const token = store.state.user.token
  if (token) {
    // 一.存在token(访问的是否是登录页)
    if (to.path === '/login') {
      next('/')// 留在当前页
    } else {
      await store.dispatch('user/getuserInfo')// 登录就获取用户信息
      next()// 放行,去登录页
    }
  } else {
  // 二.  不存在token
    if (whiteList.includes(to.path)) { // 是否在白名单
      next()// 放行
    } else {
      next('/login')// 去登录页
    }
  }
})
// 后置守卫
router.afterEach((to, from) => {
  NProgress.done()
})
